/* -----------------------------------------------------------------------------
 * PROJECT    : lwf_handlers
 * FILE       : FractionComparisonHandler.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Mar 16, 2011  mycodetalks			       Created
 */
package net.lwf.handler.math.fraction;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import lwf.handler.IProcessHandler;
import lwf.handler.ProcessHandler;
import lwf.handler.entities.RadioData;
import lwf.handler.helper.DisplayFormatter;
import net.lwf.common.constant.ICommonConstant;
import net.lwf.entity.QuestionInfo;
import net.lwf.handler.math.fraction.helper.FractionComparator;
import net.lwf.handler.math.fraction.helper.FractionHandlerHelper;
import net.lwf.jaxb.entity.Fraction;
import net.lwf.jaxb.util.maths.fraction.FractionGenerator;
import net.lwf.jaxb.util.maths.fraction.FractionUtil;
import net.lwf.pdf.util.FontUtil;
import net.lwf.pdf.util.PdfPCellUtil;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;

/**
 * Class description goes here
 */
public class FractionComparisonHandler extends ProcessHandler implements IProcessHandler {

	private FractionGenerator generator;
	
	public FractionComparisonHandler(String grade, String operation) {
		this.generator = new FractionGenerator(grade, operation);
	}
	
	@Override
	public List<QuestionInfo> generateMultiple() {
		List<QuestionInfo> questions = new ArrayList<QuestionInfo>();
		int nQuestions = getRows() * getColumns();
		for(int i=0; i<nQuestions; i++) {
			QuestionInfo question = this.generateSingle(true);
			question.setQuestionNumber(i+1);
			questions.add(question);
		}
		return questions;
	}

	@Override
	public QuestionInfo generateSingle() {
		return this.generateSingle(false);
	}

	@Override
	public int getColumns() {
		return 3;
	}

	@Override
	public void getContent(Document document) throws DocumentException {
	}

	@Override
	public String getPdfInstructionalText() {
		return "Compare the following fractions and the comparator in the box.";
	}

	@Override
	public String getPdfNote() {
		return "Use the symbol > for greater than; < for less than; = for equal to";
	}

	@Override
	public int getRows() {
		return 8;
	}

	@Override
	public boolean isPortrait() {
		return true;
	}

	public DisplayFormatter getDisplayFormatter() {
		return null;
	}

	public String getQuestionDesc() {
		return null;
	}

	public Element processCellData(QuestionInfo question) {
		
		FractionComparator comparator = (FractionComparator) question.getQuestionObject();
		PdfPTable table = new PdfPTable(3);
		float[] colsWidth = {1f, 1f, 1f}; // Code 1
		try {
			table.setWidths(colsWidth);
		} catch (DocumentException e) {
			e.printStackTrace();
		}
        table.setSpacingBefore(20);

        //Row 1
        PdfPCell cell;
        cell = new PdfPCell(new Phrase("# " + question.getQuestionNumber(), FontUtil.getQuestionNumberFont()));
        cell.setBorder(0);
        cell.setColspan(3);

        table.addCell(cell);
        
        //row 2
        //Cell 1
       // Element element = FractionHandlerHelper.generatePDfTable(comparator.getOne());
        
        cell = PdfPCellUtil.getEmptyCell(0, 0);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.addElement(FractionHandlerHelper.generatePDfTable(comparator.getOne()));
        table.addCell(cell);
        //Cell 2
        cell = PdfPCellUtil.getEmptyCell(0, 0);
        cell.setBorderWidthLeft(2);
        cell.setBorderWidthRight(2);
        cell.setBorderWidthTop(2);
        cell.setBorderWidthBottom(2);
        table.addCell(cell);

        //Row 3
        cell = PdfPCellUtil.getEmptyCell(0, 0);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.addElement(FractionHandlerHelper.generatePDfTable(comparator.getTwo()));
        table.addCell(cell);
        
        table.completeRow();

		return table;
	}
	
	private QuestionInfo generateSingle(boolean isMultiple) {
		QuestionInfo qinfo = new QuestionInfo();
		qinfo.setOperation("comparison");
		Fraction one = this.generator.generate(false);
		Fraction two = this.generator.generate(false);
		FractionComparator comparator = new FractionComparator(one, two);
		qinfo.setQuestionObject(comparator);
		qinfo.setAnswer(FractionUtil.compare(one, two));
		if(!isMultiple) {
			qinfo.setHtmlQuestion(this.generateHtmlQuestion(one, two));	
			qinfo.setRadioAnswerChoices(this.getRadioAnswers());
		}
		
		return qinfo;		
	}

	private String generateHtmlQuestion(Fraction one, Fraction two) {
		StringBuffer sbHtml = new StringBuffer("<table align=\"center\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" >");
		sbHtml.append("<tr>");
			sbHtml.append("<td>" + one.toHtml() + "</td>");
			sbHtml.append("<td> &nbsp; </td>");
			//sbHtml.append("<td valign=\"middle\" > <input type=\"text\" name=\"useranswer\" class=\"smallsizetextbox\" /> </td>");
			sbHtml.append("<td class=\"question\" > ? </td>");
			sbHtml.append("<td> &nbsp; </td>");
			sbHtml.append("<td>" + two.toHtml() + "</td>");
		sbHtml.append("</tr>");
		sbHtml.append("</table>");
		
		return sbHtml.toString();
		
	}
	
	private List<RadioData> getRadioAnswers() {
		List<RadioData> rdata = new ArrayList<RadioData>();
		
		rdata.add(new RadioData(ICommonConstant.GREATER_THAN, ICommonConstant.GREATER_THAN));
		rdata.add(new RadioData(ICommonConstant.LESS_THAN, ICommonConstant.LESS_THAN));
		rdata.add(new RadioData(ICommonConstant.EQUAL_TO, ICommonConstant.EQUAL_TO));
		
		Collections.shuffle(rdata);
		
		return rdata;
	}

}



